<?php if(!defined('CALL')) exit('No direct script access allowed');

include_once('lib/xtemplate.class.php');
include_once('lib/utils.class.php');



define("FRAMEWORK_TEMPLATE", "xtpl/page.xtpl");

abstract class Page
{
	protected $html = "";
	protected $actor = null;
	protected $user  = null;
	protected $page_name = "";
	
	protected $msgbox_head = "Информация";
	protected $msgbox_text = "<p>Здравствуй дружище!<br/>Наконец-то ты появился :)</p>";
	protected $msgbox_show = 0;
	
	abstract protected function gen_main();
	abstract protected function gen_side();
	abstract protected function add_css();
	abstract protected function add_css_files();
	abstract protected function add_js();
	abstract protected function add_js_files();
	
	public function __construct($actor, $user)
	{
		$this->actor = $actor;
		$this->user = $user;
	}
	
	protected function build_framework()
	{
		// Logger::Write(__FILE__." ".__FUNCTION__." line: ".__LINE__," username: ".$this->actor->props['username']);
		
		$xtpl = new XTemplate(FRAMEWORK_TEMPLATE);
		$xtpl->assign('title', TITLE);
		$xtpl->assign('root', ROOT_URL);
		$xtpl->assign('page_type',  $_SESSION["page"]);
		$xtpl->assign('page_categ', $_SESSION["categ"]);
		$xtpl->assign('row_num', NUM_ROWS);
		$xtpl->assign('DBG', DEBUG_MODE ? 'true' : 'false');
		$xtpl->assign('date_cmp', Utils::press_date($_SESSION["date"]));

		$xtpl->assign('actor_id', is_null($this->actor->id) ? 'null' : $this->actor->id);
		$xtpl->assign('user_id', is_null($this->user->id) ? 'null' : $this->user->id);
		$xtpl->assign('url_id', is_null($_SESSION["url_id"]) ? 'null' : $_SESSION["url_id"]);
		$xtpl->assign('city_id', is_null($_SESSION["city_id"]) ? '0' : $_SESSION["city_id"]);
		$xtpl->assign('page_name', $this->page_name);
		$xtpl->assign('avatar', $this->actor->props['face']);
		$xtpl->assign('note', $this->actor->props['note']);
		$xtpl->assign('news_num', $this->actor->news_num);
		$xtpl->assign('actor_name', $this->actor->props['username']);
		
		$this->prepare_msgbox();
		$xtpl->assign('msgbox_show', $this->msgbox_show);
		$xtpl->assign('msgbox_head', $this->msgbox_head);
		$xtpl->assign('msgbox_text', $this->msgbox_text);
		
		if(is_null($this->actor))
		{
			// ничего не показываем в правом верхнем углу
			// т.к. пользователь неавторизован
		}
		elseif($this->actor->is_authorized())
		{
			$s = implode(',', $this->actor->links['WM']);
			$xtpl->assign('users_wm', $s);
			$s = implode(',', $this->actor->links['FM']);
			$xtpl->assign('users_fm', $s);
			$s = implode(',', $this->actor->links['WS']);
			$xtpl->assign('users_ws', $s);
			$s = implode(',', $this->actor->links['FS']);
			$xtpl->assign('users_fs', $s);
			
			$xtpl->parse('root.user');
		}
		else
		{
			$xtpl->parse('root.login');
		}
		
		return $xtpl;
	}
	
	protected function prepare_msgbox()
	{
		
	}
	
	public function out()
	{
		header('Content-Type: text/html; charset=utf-8');
		// header('Content-Type: text/plain; charset=utf-8');
		$xtpl = $this->build_framework();
		
		$xtpl->assign('main', $this->gen_main());
		$xtpl->assign('side', $this->gen_side());
		$xtpl->assign('css', $this->add_css());
		$xtpl->assign('css_files', $this->add_css_files());
		$xtpl->assign('js', $this->add_js());
		$xtpl->assign('js_files', $this->add_js_files());
		
		$xtpl->parse('root');
		
		echo $xtpl->out('root');
		echo "";
	}
}
